//
//  DMWeatherView.m
//  CQKuaiYi
//
//  Created by Mr.Cannon on 2017/1/8.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMWeatherView.h"

@interface DMWeatherView ()
//天气图片
@property(nonatomic,strong)UIImageView* weatherImage;
//简化lable
@property(nonatomic,strong)UILabel* briefnessLable;
//详情lable
@property(nonatomic,strong)UILabel* complexityLable;

@end
@implementation DMWeatherView
-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupUI];
    }
    return self;
}
#pragma mark - 初始化界面
-(void)setupUI
{
    self.backgroundColor = DMRGBCOLOR;
    //创建天气图标
    UIImageView* weatherImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cloudy1"]];
    weatherImage.contentMode = UIViewContentModeScaleAspectFill;
    //创建简要lable
    UILabel* briefnessLable = [UILabel cz_labelWithText:@"26 ~ 15 °C" fontSize:16 color:[UIColor whiteColor]];
    //创建详情lable
    UILabel* complexityLable = [UILabel cz_labelWithText:@"深圳(多云)周日 01月08日 (实时:26°C)" fontSize:18 color:[UIColor whiteColor]];
    complexityLable.textAlignment = NSTextAlignmentCenter;
    [self addSubview:weatherImage];
    [self addSubview:briefnessLable];
    [self addSubview:complexityLable];
    [complexityLable mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.bottom.equalTo(self).offset(-5);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(20);
    }];
    [weatherImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(complexityLable.mas_top);
        make.centerX.equalTo(self).offset(-35);
        make.top.equalTo(self);
        make.width.mas_equalTo(20);
    }];
    [briefnessLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(30);
        make.bottom.equalTo(complexityLable.mas_top);
        make.top.equalTo(self).offset(15);
    }];
    //记录
    self.complexityLable = complexityLable;
    self.weatherImage = weatherImage;
    self.briefnessLable = briefnessLable;
}
//重写set方法刷新UI
-(void)setModel:(DMWeatherModel *)model
{
    /**
     "date": "周二 11月15日 (实时：26℃)",
     "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
     "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
     "weather": "多云",
     "wind": "微风",
     "temperature": "29 ~ 21℃"
     */
    _model = model;
    self.briefnessLable.text = model.temperature;
    NSString* SZWeath = [NSString stringWithFormat:@"深圳(%@)%@",model.weather,model.date];
    self.complexityLable.text = SZWeath;
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    NSLog(@"%@",dateString);
    //判断是否是晚上
    if(dateString.integerValue>18||dateString.integerValue<7)
    {
        NSString* weatherImg = [NSString stringWithFormat:@"%@2_night",model.weather];
        self.weatherImage.image = [UIImage imageNamed:weatherImg];
    }else
    {
        NSString* weatherImg = [NSString stringWithFormat:@"%@2",model.weather];
        self.weatherImage.image = [UIImage imageNamed:weatherImg];
    }
}
@end
